<?php
/**
 * 使用示例
 * @author M2
 * @version 1.0
 * create Date 2013-11-18
 */
//YlPageChannelsTaglib
class YlPageChannelsTaglib {
    private $nowPage = 1;
    private $count = 0;
    private $pagesize = 20;
    private $totalPage = 0;
	public function __construct() {
		C(include SITE_PATH.'/extends/taglib/tagarr.php');
	}
	public function parse($content,$siteid, $domain, $channel) {
        if (stripos($content, 'yl:pagechannels') !== false) {
            $matches = preg_parse($content, 'yl:pagechannels');
            $channelname = getParseOneAttr($matches[0], 'channelname');
            $pagesize = getParseOneAttr($matches[0], 'pagesize');

            if(empty($channelname)) {
                return $content;
            }
            //update next 2013-12-12 增加单栏目标签缓存
            if (($findcate = S('cache_channelname_'.$siteid.'_'.$channelname)) === false) {
                $findcate = M('cms_category')
                    ->field('(case when url <> "" then url when createurl <> "" then createurl
                            else (concat("'.$domain.'","/",replace(replace((case when catenaming <> ""
                         then catenaming else ("'.$channel['catenaming'].'") end),"{cateid}",id),"{@ChannelID}",id))) end) as url,
                            replace(imageurl,"@","'.$domain.'") as imageurl,id,cname,content,keywords,
                            description')
                    ->where('siteid = '.$siteid.' and cname="'.$channelname.'"')
                    ->find();
                S('cache_channelname_'.$siteid.'_'.$channelname, $findcate);
            }

            $data['pid'] = $findcate['id'];
            $this->count = M('cms_category')->where($data)->count(); //统计当前栏目下的所有子栏目总数
            $this->pagesize = $pagesize ? $pagesize : $this->pagesize;//获取并设置每页显示多少条
            $this->totalPage = ceil($this->count/$this->pagesize); //总页数
            if($this->nowPage >= $this->totalPage) $this->nowPage = $this->totalPage; //如果当前页大于总页数，就让当前页=总页数
            if($this->nowPage<=1) $this->nowPage = 1; //如果当前页小于1，当前页=1

            $result = M('cms_category')
                ->field('(case when url <> "" then url when createurl <> "" then createurl
                      else (concat("'.$domain.'","/",replace(replace((case when catenaming <> ""
                     then catenaming else ("'.$channel['catenaming'].'") end),"{cateid}",id))) end) as url,
                      replace(imageurl,"@","'.$domain.'") as imageurl,id,cname,content,keywords,
                      description')
                ->where($data)->select(); //获取内容列表

            for($i=1;$i<=$this->totalPage;$i++) {
                $this->nowPage = $i;
                $start = $this->pagesize * ($this->nowPage -1); //获取起始页
                $list = array_slice($result, $start, $this->pagesize);
                $parseStr = '';
                $pageChannelsText = getParseContentText($matches[0], 'yl:pagechannels');
                if(!empty($list)) {
                    foreach ($list as $k => $v) {
                        S('cache_channel_'.$v['id'], $v);
                        //update next 2013-12-12 简化替换操作
                        $text = taglib(C('yl:if'))->parse($pageChannelsText, $v);
                        $text = taglib(C('yl:a'))->parse($text,$siteid,$v,'2',$domain,$channel);
                        $text = taglib(C('yl:channel'))->parse($text,$v,$siteid,$domain, $channel);
                        $text = taglib(C('yl:image'))->parse($text,$v);
                        $text = taglib(C('yl:count'))->parse($text,$v);

                        if(strpos($text,'{') && strpos($text,'}')) {
                            preg_match_all('/\{Channel.(.*?)\}/eis',$text,$out);
                            for ($i=0; $i< count($out[1]); $i++) {
                                $text = str_ireplace($out[0][$i],$v[$out[1][$i]],$text);
                            }
                        }
                        $parseStr .= $text;
                    }
                } else {
                    $parseStr = '';
                }
                $pageChannel = str_replace($matches[0], $parseStr, $content);
                $matchesItems = preg_parse($pageChannel, 'yl:pageItems');
                foreach($matchesItems as $fp) {
                    $text = getParseContentText($fp, 'yl:pageItems');
                    $matchesItem = preg_parse($text, 'yl:pageitem');

                    foreach($matchesItem as $epageitem) {
                        $text = taglib(C('yl:pageitem'))->parse($epageitem,$findcate,$this->count,$this->pagesize,$this->nowPage);
                        $pageChannel = str_replace($epageitem, $text, $pageChannel);
                    }
                    $qtext = getParseContentText($fp, 'yl:pageItems');
                    $pageChannel = str_replace($fp, $qtext, $pageChannel);
                }
                $catenaming = common('Array')->getParseTemplate($findcate,$findcate['catenaming'],'cateid');
                $arr = explode('.',$catenaming);
                if($this->nowPage == 1)
                    $contents['catenaming'] = $arr[0].'.'.$arr[1];
                else
                    $contents['catenaming'] = $arr[0].'-'.$this->nowPage.'.'.$arr[1];
                $templatePath = D('Site')->getSiteInfo($siteid);
                $path = $templatePath.$contents['catenaming'];
                file_put_contents($path,$pageChannel);
                //OOO  add by jepin id去除重复
                clearIds();
                //OOO  end
            }
            return '';
        }
        return $content;
	}
}